#
# Copyright (c) 2010 David Hamrick, Guy Kopsombut, Emily Ross 
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import httplib
import json

def MakeGetRequest(path):
	conn = httplib.HTTPConnection("localhost",8080)
	conn.request("GET",path)
	r1 = conn.getresponse()
	return r1.read()
	
def MakePutRequest(path,body):
	conn = httplib.HTTPConnection("localhost",8080)
	conn.request("PUT",path,body)
	r1 = conn.getresponse()
	return r1.read()
	
def MakePostRequest(path,body):
	conn = httplib.HTTPConnection("localhost",8080)
	conn.request("POST",path,body)
	r1 = conn.getresponse()
	return r1.read()

def MakeDeleteRequest(path):
	conn = httplib.HTTPConnection("localhost",8080)
	conn.request("DELETE",path)
	r1 = conn.getresponse()
	return r1.read()
	
def UnitTest():

	#Test 1 - PUT to create a new cab
	passed = True;
	jsonStr = MakePutRequest("/cabs",' ')
	cabobj = json.loads(jsonStr)
	passed = passed and cabobj['name'] == 'null'
	passed = passed and cabobj['latitude'] == 'null' 
	passed = passed and cabobj['longitude'] == 'null'
	if passed:
		print 'TEST 1\t\t\tPASSED'
	else:
		print 'TEST 1\t\t\tFAILED'
		return
	
	#Test 2 - GET the cab you just created
	jsonStr = MakeGetRequest("/cabs/%d"%(cabobj['id']))
	cabobj = json.loads(jsonStr)
	passed = passed and cabobj['name'] == 'null'
	passed = passed and cabobj['latitude'] == 'null'
	passed = passed and cabobj['longitude'] == 'null'
	if passed:
		print 'TEST 2\t\t\tPASSED'
	else:
		print 'TEST 2\t\t\tFAILED'
		return
	
	
	#Test 3 - POST to update the cab
	cabobj['name'] = 'Cab000'
	cabobj['latitude'] = '1234'
	cabobj['longitude'] = '4321'
	jsonStr = MakePostRequest("/cabs/%d"%(cabobj['id']),json.dumps(cabobj))
	cabobj = json.loads(jsonStr)
	passed = passed and cabobj['name'] == 'Cab000'
	passed = passed and cabobj['latitude'] == '1234'
	passed = passed and cabobj['longitude'] == '4321'
	if passed:
		print 'TEST 3\t\t\tPASSED'
	else:
		print 'TEST 3\t\t\tFAILED'
		return
		
	#Test 4 - GET to see that the cab did in fact update
	jsonStr = MakeGetRequest("/cabs/%d"%(cabobj['id']))
	cabobj = json.loads(jsonStr)
	passed = passed and cabobj['name'] == 'Cab000'
	passed = passed and cabobj['latitude'] == '1234'
	passed = passed and cabobj['longitude'] == '4321'
	if passed:
		print 'TEST 4\t\t\tPASSED'
	else:
		print 'TEST 4\t\t\tFAILED'
		return
	
	#Test 5 - DELETE the cab
	cabid = cabobj['id']
	jsonStr = MakeDeleteRequest("/cabs/%d"%(cabid))
	cabobj = json.loads(jsonStr)
	passed = passed and cabobj['id'] == cabid
	passed = passed and cabobj['delete-status'] == 'deleted'
	if passed:
		print 'TEST 5\t\t\tPASSED'
	else:
		print 'TEST 5\t\t\tFAILED'
		return

	#Test 6 - GET the cab you just deleted, check error code
	jsonStr = MakeGetRequest("/cabs/%d"%(cabid))
	cabobj = json.loads(jsonStr)
	passed = passed and cabobj['error'] == 404
	if passed:
		print 'TEST 6\t\t\tPASSED'
	else:
		print 'TEST 6\t\t\tFAILED'
		return

	#Test 7 - GET a cab with an characters in the id
	jsonStr = MakeGetRequest("/cabs/abc")
	cabobj = json.loads(jsonStr)
	passed = passed and cabobj['error'] == 404
	if passed:
		print 'TEST 7\t\t\tPASSED'
	else:
		print 'TEST 7\t\t\tFAILED'
		
	#Test 8 - Try DELETING a cab that doesn't exist
	jsonStr = MakeDeleteRequest("/cabs/78789999")
	cabobj = json.loads(jsonStr)
	passed = passed and cabobj['error'] == 404
	if passed:
		print 'TEST 8\t\t\tPASSED'
	else:
		print 'TEST 8\t\t\tFAILED'
		return
	
	#Test 9 - GET when there are 0 passengers
	jsonStr = MakeGetRequest("/passengers")
	passObj = json.loads(jsonStr)
	passed = passed 
# and passObj == 'null'
	if passed:
		print 'TEST 9\t\t\tPASSED'
	else:
		print 'TEST 9\t\t\tFAILED'

	#Test 10 - PUT a passenger
	passObj = dict();
	passObj['name'] = 'David'
	passObj['address'] = '2600 Hillsboro Pike'
	passObj['phone'] = '555-555-5555'
	passObj['cabid'] = 'null'
	passObj['status'] = 'unassigned'
	jsonStr = MakePutRequest("/passengers",json.dumps(passObj))
	if jsonStr:
		passObj = json.loads(jsonStr)
		passed = passed and passObj['name'] == 'David'
		passed = passed and passObj['address'] == '2600 Hillsboro Pike'
		passed = passed and passObj['phone'] =='555-555-5555'
		passed = passed and passObj['cabid'] == 'null'
		passed = passed and passObj['status'] == 'unassigned'
	else:
		passed = False
	if passed:
		print 'TEST 10\t\t\tPASSED'
	else:
		print 'TEST 10\t\t\tFAILED'
		return
		
	#Test 11 - GET when there is only 1 passenger
	'''
	jsonStr = MakeGetRequest("/passengers")
	if jsonStr:
		passList = json.loads(jsonStr)
		passObj = passList[0]
		passed = passed and passObj['name'] == 'David'
		passed = passed and passObj['address'] == '2600 Hillsboro Pike'
		passed = passed and passObj['phone'] =='555-555-5555'
		passed = passed and passObj['cabid'] == 'null'
		passed = passed and passObj['status'] == 'unassigned'
	else:
		passed = False
	if passed:
		print 'TEST 11\t\t\tPASSED'
	else:
		print 'TEST 11\t\t\tFAILED'
		return	
	
	#Test 12 - GET the unassigned passenger
	jsonStr = MakeGetRequest("/passengers?status=unassigned")
	if jsonStr:
		passList = json.loads(jsonStr)
		passObj = passList[0]
		passed = passed and passObj['name'] == 'David'
		passed = passed and passObj['address'] == '2600 Hillsboro Pike'
		passed = passed and passObj['phone'] =='555-555-5555'
		passed = passed and passObj['cabid'] == 'null'
		passed = passed and passObj['status'] == 'unassigned'
	else:
		passed = False
	if passed:
		print 'TEST 12\t\t\tPASSED'
	else:
		print 'TEST 12\t\t\tFAILED'
		return	

	#Test 13 - POST to update the passenger
	passObj['name'] = 'David'
	passObj['address'] = '2605 Hillsboro Pike'
	passObj['phone'] = '555-555-5555'
	passObj['cabid'] = '1234'
	passObj['status'] = 'assigned'
	jsonStr = MakePostRequest("/passengers/%d"%(passObj['id']),json.dumps(passObj))
	passObj = json.loads(jsonStr)
	passed = passed and passObj['name'] == 'David'
	passed = passed and passObj['address'] == '2605 Hillsboro Pike'
	passed = passed and passObj['phone'] == '555-555-5555'
	passed = passed and passObj['cabid'] == '1234'
	passed = passed and passObj['status'] == 'assigned'
	if passed:
		print 'TEST 13\t\t\tPASSED'
	else:
		print 'TEST 13\t\t\tFAILED'
		return
	
	#Test 14 - GET the updated passenger
	jsonStr = MakeGetRequest("/passengers/%d"%(passObj['id']))
	#passObj = json.loads(jsonStr)
	passed = passed and passObj['name'] == 'David'
	passed = passed and passObj['address'] == '2605 Hillsboro Pike'
	passed = passed and passObj['phone'] == '555-555-5555'
	passed = passed and passObj['cabid'] == '1234'
	passed = passed and passObj['status'] == 'assigned'
	if passed:
		print 'TEST 14\t\t\tPASSED'
	else:
		print 'TEST 14\t\t\tFAILED'
		return

	#Test _ - DELETE the passenger

	#Test _ - GET a passenger that doesn't exist

	#Test _ - DELETE a passenger that doesn't exist
	'''

#main	
UnitTest()
